﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RaytracerLib.Layers.OverlayCorrectors
{
    public class MirrorOverlayCorrector : OverlayCorrector
    {
        public MirrorOverlayCorrector(int a_width, int a_height, int a_margin)
            : base(a_width, a_height, a_margin)
        {
        }

        public override double CorrectUV(double a_uv)
        {
            return MirrorOverlayCorrector.Correct_UV(a_uv);
        }

        public static double Correct_UV(double a_uv)
        {
            if (a_uv < 0)
                a_uv = -a_uv;
            if (a_uv >= 1)
            {
                a_uv = a_uv % 2;
                if (a_uv >= 1)
                    return 2 - a_uv;
            }
            return a_uv;
        }

        protected override int CorrectXY(int a_xy, int a_size)
        {
            return MirrorOverlayCorrector.Correct_XY(a_xy, a_size);
        }

        public static int Correct_XY(int a_xy, int a_size)
        {
            if (a_xy < 0)
                a_xy = -a_xy - 1;
            if (a_xy >= a_size)
            {
                a_xy = a_xy % (2 * a_size);

                if (a_xy >= a_size)
                    a_xy = (2 * a_size) - a_xy - 1;
            }
            return a_xy;
        }
    }
}
